﻿#include "DialogHirfExtrapolation.h"
#include "ui_DialogHirfExtrapolation.h"

#include "AnimationSettingData.h"
#include "DataChosenListDialog.h"
#include <QIntValidator>
#include "IdaWidgetHelper.h"
#include "IdaDialogMsg.h"
#include <QListView>
#include <QDebug>
#include "BaseModule/IBaseSignal.h"

namespace pst
{
    DialogHirfExtrapolation::DialogHirfExtrapolation(QWidget* parent)
        : IdaDialogFrame(parent), ui(new Ui::DialogHirfExtrapolation())
    {
        ui->setupUi(this);

        setWindowTitle(tr("HIRF外推"));
        IdaWidgetHelperInstance->setMainButtonStyle(ui->pushButton_confirm); // 将确定设置为主按钮

        ui->pushButton_SelcetBarElectricField->setIcon(QIcon(":/rc/apostrophe.png"));
        ui->pushButton_SelcetBarElectricField->setStyleSheet("QPushButton { text-align: center; }");
        ui->pushButton_SelcetBarElectricCurrent->setIcon(QIcon(":/rc/apostrophe.png"));
        ui->pushButton_SelcetBarElectricCurrent->setStyleSheet("QPushButton { text-align: center; }");

        connect(ui->pushButton_SelcetBarElectricField, &QPushButton::clicked, this, &DialogHirfExtrapolation::slot_selectDataButtonElectricFieldClicked);
        connect(ui->pushButton_SelcetBarElectricCurrent, &QPushButton::clicked, this, &DialogHirfExtrapolation::slot_selectDataButtonElectricCurrentClicked);
        connect(ui->radioButton_ElectricField, &QRadioButton::clicked, this, &DialogHirfExtrapolation::slot_radioButton_ElectricFieldClicked);
        connect(ui->radioButton_ElectricCurrent, &QRadioButton::clicked, this, &DialogHirfExtrapolation::slot_radioButton_ElectricCurrentClicked);
        ui->radioButton_ElectricField->setChecked(true);
        slot_radioButton_ElectricFieldClicked();
        ui->doubleSpinBox_ElectricField->setEnabled(false);
        ui->doubleSpinBox_ElectricCurrent->setEnabled(false);
        ui->comboBox_ExtrapolationCoefficient->setView(new QListView());

        double m_minValue = 1E-9;
        double m_maxValue = 1E9;

        ui->doubleSpinBox_ElectricField->setRange(m_minValue, m_maxValue);
        ui->doubleSpinBox_ElectricField->setDecimals(9);
        ui->doubleSpinBox_ElectricCurrent->setRange(m_minValue, m_maxValue);
        ui->doubleSpinBox_ElectricCurrent->setDecimals(9);
    }

    DialogHirfExtrapolation::~DialogHirfExtrapolation()
    {
        delete ui;
    }

    void DialogHirfExtrapolation::setCurveNameList(const QList<QString>& curveNameList)
    {
        m_curveNameList = curveNameList;
        m_model.setStringList(m_curveNameList);
    }

    void DialogHirfExtrapolation::setInit(QStringList& params)
    {
        if (params.at(0) != "0")// 与DataAnalysisType保持一致
            return;
        params.removeFirst();
        if (params.at(2) == "E_total")
        {
            slot_radioButton_ElectricFieldClicked();
            ui->doubleSpinBox_ElectricField->setValue(params.at(1).toDouble());
            ui->checkBox_ElectricField->setChecked(params.at(4).toInt());
        }
        else if (params.at(2) == "I")
        {
            slot_radioButton_ElectricCurrentClicked();
            ui->doubleSpinBox_ElectricCurrent->setValue(params.at(1).toDouble());
            ui->checkBox_ElectricCurrent->setChecked(params.at(4).toInt());
        }
        if (params.at(3) == "HIRF环境1")
            ui->comboBox_ExtrapolationCoefficient->setCurrentIndex(0);
        else if (params.at(3) == "HIRF环境2")
            ui->comboBox_ExtrapolationCoefficient->setCurrentIndex(1);

        slot_sentChosenDataIds(createSelectedIndexes(QStringList() << params.at(0)));
    }

    void DialogHirfExtrapolation::setExtrapolationCoefficientList(const QList<QString>& extrapolationCoefficientList)
    {
        ui->comboBox_ExtrapolationCoefficient->addItems(extrapolationCoefficientList);
    }

    void DialogHirfExtrapolation::on_pushButton_confirm_clicked()
    {
        ui->pushButton_confirm->setEnabled(false);
        double _incidentWaveFieldValue{};
        if (m_isElectricFieldChecked)
        {
            if (m_selectedIndexsField.isEmpty())
            {
                auto _text = QString("未选择任何曲线，请选择曲线！");
                auto _res = IdaDialogMsg::showDialog(nullptr, _text, EDialogMsg::Warning, QStringLiteral("警告"), {QStringLiteral("确定")});
                ui->pushButton_confirm->setEnabled(true);
                return;
            }
            _incidentWaveFieldValue = ui->doubleSpinBox_ElectricField->value();
            if (qFuzzyCompare(_incidentWaveFieldValue, 0.0000000000))
            {
                auto _text = QString("入射波场值应大于0，请重新输入！");
                auto _res = IdaDialogMsg::showDialog(nullptr, _text, EDialogMsg::Warning, QStringLiteral("警告"), {QStringLiteral("确定")});
                ui->pushButton_confirm->setEnabled(true);
                return;
            }
        }
        else
        {
            if (m_selectedIndexsCurrent.isEmpty())
            {
                auto _text = QString("未选择任何曲线，请选择曲线！");
                auto _res = IdaDialogMsg::showDialog(nullptr, _text, EDialogMsg::Warning, QStringLiteral("警告"), {QStringLiteral("确定")});
                ui->pushButton_confirm->setEnabled(true);
                return;
            }
            _incidentWaveFieldValue = ui->doubleSpinBox_ElectricCurrent->value();
            if (qFuzzyCompare(_incidentWaveFieldValue, 0.0000000000))
            {
                auto _text = QString("入射波场值应大于0，请重新输入！");
                auto _res = IdaDialogMsg::showDialog(nullptr, _text, EDialogMsg::Warning, QStringLiteral("警告"), {QStringLiteral("确定")});
                ui->pushButton_confirm->setEnabled(true);
                return;
            }
        }

        QString typeName;
        bool chekBoxStatus = false; 
        if (ui->pushButton_SelcetBarElectricField->isEnabled())
        {
            typeName = "E_total";
            chekBoxStatus = ui->checkBox_ElectricField->isChecked();
        }
        else if (ui->pushButton_SelcetBarElectricCurrent->isEnabled())
        {
            typeName = "I";
            chekBoxStatus = ui->checkBox_ElectricCurrent->isChecked();
        }
        emit bool isSUc = signal_computeHIRF(m_curveNameText, _incidentWaveFieldValue, m_ExtrapolationCoefficientText, typeName, chekBoxStatus);
        if (isSUc)
        {
            QString _pyCode = QString("PostProcessing.lineChartComputeHIRF('%1', %2, '%3', '%4', %5)").arg(m_curveNameText).arg(_incidentWaveFieldValue).arg(m_ExtrapolationCoefficientText).arg(typeName).arg(chekBoxStatus).remove(QRegExp("[\r\n]"));
                emit IBaseSignalInstance->signal_execPYCode(QStringList{_pyCode}, true, false);
            ui->pushButton_confirm->setEnabled(true);
            this->close();
        }
    }

    void DialogHirfExtrapolation::on_pushButton_cancel_clicked()
    {
        this->close();
    }

    void DialogHirfExtrapolation::slot_sentChosenDataIds(const QModelIndexList& chosenID)
    {
        if (m_isElectricFieldChecked)
            m_selectedIndexsField.clear();
        else
            m_selectedIndexsCurrent.clear();
        auto count = chosenID.count();
        QString strText{};
        for (int i = 0; i < count; ++i)
        {
            auto chosenData = chosenID.at(i);
            if (chosenData.isValid())
            {
                auto str = chosenData.data().toString();
                strText += str + "\n";

                auto index = chosenID.at(i).row();
                if (m_isElectricFieldChecked)
                    m_selectedIndexsField.append(index);
                else
                    m_selectedIndexsCurrent.append(index);
            }
        }
        m_curveNameText = strText;

        if (m_isElectricFieldChecked)
        {
            if (!m_selectedIndexsField.isEmpty())
                IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_SelcetBarElectricField, true, Qt::blue);
            else
                IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_SelcetBarElectricField, false, Qt::white);
        }
        else
        {
            if (!m_selectedIndexsCurrent.isEmpty())
                IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_SelcetBarElectricCurrent, true, Qt::blue);
            else
                IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_SelcetBarElectricCurrent, false, Qt::white);
        }
    }

    void DialogHirfExtrapolation::slot_selectDataButtonElectricFieldClicked()
    {
        m_dataChosenListDialog = new DataChosenListDialog();
        m_dataChosenListDialog->setFixedSize(300, 280);
        // m_dataChosenListDialog->moveToCenter();
        m_dataChosenListDialog->setData(m_curveNameList);
        m_dataChosenListDialog->setMultipleSelected(false);
        m_dataChosenListDialog->updateUI();
        m_dataChosenListDialog->showWindow();
        m_dataChosenListDialog->setSelctedIndexs(m_selectedIndexsField);                                                    // 默认选中第一个
        //IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_SelcetBarElectricField, true, Qt::blue); // 因为有默认选项

        connect(m_dataChosenListDialog,
                &DataChosenListDialog::signal_sentChosenDataIds,
                this, &DialogHirfExtrapolation::slot_sentChosenDataIds);
    }

    void DialogHirfExtrapolation::slot_selectDataButtonElectricCurrentClicked()
    {
        m_dataChosenListDialog = new DataChosenListDialog();
        m_dataChosenListDialog->setFixedSize(300, 280);
        // m_dataChosenListDialog->moveToCenter();
        m_dataChosenListDialog->setData(m_curveNameList);
        m_dataChosenListDialog->setMultipleSelected(false);
        m_dataChosenListDialog->updateUI();
        m_dataChosenListDialog->showWindow();
        m_dataChosenListDialog->setSelctedIndexs(m_selectedIndexsCurrent);                                                    // 默认选中第一个
        //IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_SelcetBarElectricCurrent, true, Qt::blue); // 因为有默认选项

        connect(m_dataChosenListDialog,
                &DataChosenListDialog::signal_sentChosenDataIds,
                this, &DialogHirfExtrapolation::slot_sentChosenDataIds);
    }

    void DialogHirfExtrapolation::on_checkBox_ElectricField_toggled(bool checked)
    {
        ui->doubleSpinBox_ElectricField->setEnabled(checked);
    }
    void DialogHirfExtrapolation::on_checkBox_ElectricCurrent_toggled(bool checked)
    {
        ui->doubleSpinBox_ElectricCurrent->setEnabled(checked);
    }

    void DialogHirfExtrapolation::on_comboBox_ExtrapolationCoefficient_currentIndexChanged(int index)
    {
        m_ExtrapolationCoefficientText = ui->comboBox_ExtrapolationCoefficient->currentText();
    }

    void DialogHirfExtrapolation::slot_radioButton_ElectricFieldClicked()
    {
        m_isElectricFieldChecked = true;
        ui->pushButton_SelcetBarElectricField->setEnabled(true);
        ui->checkBox_ElectricField->setEnabled(true);
        // ui->checkBox_ElectricField->setChecked(true);

        ui->doubleSpinBox_ElectricField->setEnabled(ui->checkBox_ElectricField->isChecked());
        ui->pushButton_SelcetBarElectricCurrent->setEnabled(false);
        ui->checkBox_ElectricCurrent->setEnabled(false);
        // ui->checkBox_ElectricCurrent->setChecked(false);
        ui->doubleSpinBox_ElectricCurrent->setEnabled(false);

        ui->radioButton_ElectricField->setChecked(true);
        ui->radioButton_ElectricCurrent->setChecked(false);
    }
    void DialogHirfExtrapolation::slot_radioButton_ElectricCurrentClicked()
    {
        m_isElectricFieldChecked = false;
        ui->pushButton_SelcetBarElectricField->setEnabled(false);
        ui->checkBox_ElectricField->setEnabled(false);
        // ui->checkBox_ElectricField->setChecked(false);
        ui->doubleSpinBox_ElectricField->setEnabled(false);
        ui->pushButton_SelcetBarElectricCurrent->setEnabled(true);
        ui->checkBox_ElectricCurrent->setEnabled(true);
        // ui->checkBox_ElectricCurrent->setChecked(true);
        ui->doubleSpinBox_ElectricCurrent->setEnabled(ui->checkBox_ElectricCurrent->isChecked());

        ui->radioButton_ElectricField->setChecked(false);
        ui->radioButton_ElectricCurrent->setChecked(true);
    }

    QModelIndexList DialogHirfExtrapolation::createSelectedIndexes(const QStringList& params) {
        QModelIndexList selectedIndexes;

        foreach(const QString & param, params) {
            int row = m_curveNameList.indexOf(param);
            if (row != -1) {
                // 使用成员模型m_model创建索引，确保索引与模型生命周期一致
                QModelIndex index = m_model.index(row, 0);
                if (index.isValid()) {
                    selectedIndexes.append(index);
                }
            }
        }
        return selectedIndexes;
    }
} // namespace pst
